<?php 
session_start();
        function findsubarray($array=array(),$parentid){
            $rs=array();
            foreach ($array as $a){
                if($parentid==$a["parentid"])
                $rs[]=$a;
            }
            return $rs;
        }
        function finditem($array=array(),$id){
            
            foreach ($array as $a){
                if($id==$a["id"])
                return $a;
            }
            return null;
        }
class customer extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('javascript');
            $this->load->library('session');
            $this->load->library('encrypt');
            $this->load->library('smarty3','','smarty');
            $this->_configs["title"]="Quản Lý Deal";
            $this->_configs["authority"]="deal";
            $this->_configs["MinAuthority"]=0;
            
            $this->load->model('deal/category_model','category_model');
            $this->load->model('deal/product_model','product_model');
            $this->load->model('deal/order_model','order_model');
            $this->load->model('admin-planners/region_model','region_model');
            $_SESSION["DRegion"]=isset($_SESSION["DRegion"])?$_SESSION["DRegion"]:"";
            $_SESSION["DRegionName"]=isset($_SESSION["DRegionName"])?$_SESSION["DRegionName"]:"Toàn Quốc";
            
            if(!isset($_SESSION["D-USER"]))LoginError ();
        }
        public function index()
	{
            $Configs=null;
            $Configs["menu"]="customer";
            $Configs["U"]["avata"]="default";
            $Configs["dealtype"]="";
            $Configs["U"]["email"]="";
            if(isset($_SESSION["D-USER"])){$Configs["U"]=$_SESSION["D-USER"];}
            $Configs["Foot"]=@file_get_contents(APPPATH."HTML_DOM/Foot.txt");
            
            $this->smarty->assign('data', $data);
            $this->smarty->assign('Configs', $Configs);
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->view     (   "deal/subs/00_css"       ,"dcss"         );
            $this->smarty->view     (   "deal/subs/00_script"    ,"dscript"      );
            $this->smarty->view     (   "deal/subs/01_head"      ,"dhead"        );
            $this->smarty->view     (   "deal/subs/02_menu"      ,"dmenu"        );
            $this->smarty->view     (   "deal/subs/03_foot"      ,"dfoot"        );
            $this->smarty->view     (   "deal/pages/04_guide"     ,"dpage"        ); 
            $this->smarty->display  (   "deal/12_information"                          );
	}
        public function information()
	{
            //echo json_encode(array("Addresss"=>Array("Region"=>"1-h-noi","Co"=>"1-h-noi-q-ba-nh","subCo"=>"")));
            if(!isset($_SESSION["D-USER"]))show_404 ();
            $Configs=null;
            $Configs["menu"]="customer";
            $Configs["dealtype"]="";
            $Configs["U"]["avata"]="default";
            $Configs["U"]["email"]="";
            if(isset($_SESSION["D-USER"])){$Configs["U"]=$_SESSION["D-USER"];}
            //$CFregion= objectToArray(json_decode(@file_get_contents(APPPATH."HTML_DOM/CFregion.txt")));
            
            //$CFregion=  objectToArray($dt);
            $OtherInfo=  objectToArray(json_decode($_SESSION["D-USER"]["Note"]));
            $Configs["Foot"]=@file_get_contents(APPPATH."HTML_DOM/Foot.txt");
            $data["regions"]=  objectToArray($this->region_model->getByParent("0"));
            $region=isset($OtherInfo["AddInfo"]["Region"])?$OtherInfo["AddInfo"]["Region"]:"";
            $co=isset($OtherInfo["AddInfo"]["Co"])?$OtherInfo["AddInfo"]["Co"]:"";
            $subco=isset($OtherInfo["AddInfo"]["subCo"])?$OtherInfo["AddInfo"]["subCo"]:"";
            
           
            
            $Configs["AddInfo"]=$OtherInfo["AddInfo"];
            $Configs["AddInfo"]["Region"]=isset($OtherInfo["AddInfo"]["Region"])?$OtherInfo["AddInfo"]["Region"]:"";
            $Configs["AddInfo"]["Co"]=isset($OtherInfo["AddInfo"]["Co"])?$OtherInfo["AddInfo"]["Co"]:"";
            $Configs["AddInfo"]["subCo"]=isset($OtherInfo["AddInfo"]["subCo"])?$OtherInfo["AddInfo"]["subCo"]:"";
            
            $city=  objectToArray($this->region_model->getByParent(0));//findsubarray($CFregion,"1");
            $subcity=isset($OtherInfo["AddInfo"]["Region"])?objectToArray($this->region_model->getByParent($Configs["AddInfo"]["Region"])):array();
            $ssubcity=isset($OtherInfo["AddInfo"]["Co"])?objectToArray($this->region_model->getByParent($Configs["AddInfo"]["Co"])):array();
            
            $Configs["AddInfo"]["strRegion"]=  finditem($city,$region);
            $Configs["AddInfo"]["strCo"]=  finditem($subcity,$co);
            $Configs["AddInfo"]["strsubCo"]=  finditem($ssubcity,$subco);
            
            $Configs["AddInfo"]["NumHouse"]=isset($Configs["AddInfo"]["NumHouse"])?$Configs["AddInfo"]["NumHouse"]:"";
            
            
            $data["city"]=$city;
            $data["subcity"]=$subcity;
            $data["ssubcity"]=$ssubcity;
            $this->smarty->assign('data', $data);
            $this->smarty->assign('Configs', $Configs);
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->view     (   "deal/subs/00_css"       ,"dcss"         );
            $this->smarty->view     (   "deal/subs/00_script"    ,"dscript"      );
            $this->smarty->view     (   "deal/subs/01_head"      ,"dhead"        );
            $this->smarty->view     (   "deal/subs/02_menu"      ,"dmenu"        );
            $this->smarty->view     (   "deal/subs/03_foot"      ,"dfoot"        );
            $this->smarty->view     (   "deal/pages/12_information"     ,"dpage"        ); 
            $this->smarty->display  (   "deal/01_home"                          );
	}
        function initsubcity(){
            $parentid=$_GET["parentid"];
            //$CFregion= objectToArray(json_decode(@file_get_contents(APPPATH."HTML_DOM/CFregion.txt")));
            
            
            $subcity=objectToArray($this->region_model->getByParent($parentid));//findsubarray($CFregion,$parentid);
            echo "<select id='cbx-co' class='cyrle' style='width: 160px;margin-right: 20px' onchange='subcitychange($(this).val());'>
                        <option value=''>[---Chọn Quận---]</option>";
            if(count($subcity)>0){
                foreach ($subcity as $c){
                     echo '<option value="'.$c["id"].'">'.$c["name"].'</option>';
                }
                
                
            }
            echo "</select>";
            
        }
        function initssubcity(){
            $parentid=$_GET["parentid"];
            //$CFregion= objectToArray(json_decode(@file_get_contents(APPPATH."HTML_DOM/CFregion.txt")));
            $subcity=$subcity=objectToArray($this->region_model->getByParent($parentid));//findsubarray($CFregion,$parentid);
            
            echo "<select id='cbx-subco' class='cyrle' style='width: 160px' onchange='changeshipprice($(this).val());'>
                        <option value=''>[---Chọn Phường---]</option>";
                
            if(count($subcity)>0){
                foreach ($subcity as $c){
                     echo '<option value="'.$c["id"].'">'.$c["name"].'</option>';
                }
                
                
            }
            echo "</select>";
            
        }
        function changeaddinfo(){
            $this->load->model('deal/customer_model','customer_model');
            $code=-1;
            $msg=  json_encode($_POST["AddInfo"]);
            $note=array("AddInfo"=>$_POST["AddInfo"]);
            $params["Note"]=  json_encode($note);
            
            if($this->customer_model->update($_SESSION["D-USER"]["CustomerID"],$params)){
                $code=1;
                $msg=$msg;
                $_SESSION["D-USER"]["Note"]=$params["Note"];
                
            }else{
                $code=-1;
                $msg="Cập nhật thông tin thất bại";
            }
            $rs=array("code"=>$code,"msg"=>$msg);
            echo json_encode($rs);
        }
        function changeaccinfo(){
            $this->load->model('deal/customer_model','customer_model');
            $code=-1;
            $msg=  json_encode($_POST["AccInfo"]);
            $AccInfo=$_POST["AccInfo"];
            
            
            if($this->customer_model->update($_SESSION["D-USER"]["CustomerID"],$AccInfo)){
                $code=1;
                $msg=$msg;
                $_SESSION["D-USER"]["CustomerName"]=$AccInfo["CustomerName"];
                $_SESSION["D-USER"]["Sex"]=$AccInfo["Sex"];
                $_SESSION["D-USER"]["Phone"]=$AccInfo["Phone"];
                $_SESSION["D-USER"]["Birth"]=$AccInfo["Birth"];
                
                
            }else{
                $code=-1;
                $msg="Cập nhật thông tin thất bại";
            }
            $rs=array("code"=>$code,"msg"=>$msg);
            echo json_encode($rs);
        }
        public function orders(){
            if(!isset($_SESSION["D-USER"]))show_404 ();
            $Configs=null;
            $Configs["menu"]="customer";
            $Configs["dealtype"]="";
            $Configs["U"]["avata"]="default";
            $Configs["U"]["email"]="";
            if(isset($_SESSION["D-USER"])){
                $Configs["U"]=$_SESSION["D-USER"];
                
            }
            $Orders=  objectToArray($this->order_model->getByCuss($_SESSION["D-USER"]["CustomerID"]));
            for ($i=0;$i<count($Orders);$i++){
                $Orders[$i]["OrderIDEndcode"]=$this->encrypt->encode($Orders[$i]["OrderID"]);
                $Orders[$i]["OrderDetail"]=  objectToArray(json_decode($Orders[$i]["OrderDetail"]));
            }
            $data["regions"]=  objectToArray($this->region_model->getByParent("0"));
            $Configs["Foot"]=@file_get_contents(APPPATH."HTML_DOM/Foot.txt");
            $this->smarty->assign('data', $data);
            $this->smarty->assign('Orders', $Orders);
            $this->smarty->assign('Configs', $Configs);
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->view     (   "deal/subs/00_css"       ,"dcss"         );
            $this->smarty->view     (   "deal/subs/00_script"    ,"dscript"      );
            $this->smarty->view     (   "deal/subs/01_head"      ,"dhead"        );
            $this->smarty->view     (   "deal/subs/02_menu"      ,"dmenu"        );
            $this->smarty->view     (   "deal/subs/03_foot"      ,"dfoot"        );
            $this->smarty->view     (   "deal/pages/11_orders"     ,"dpage"        ); 
            $this->smarty->display  (   "deal/01_home"                          );
        }
        public function changepass(){
            $Configs=null;
            $Configs["menu"]="customer";
            $Configs["dealtype"]="";
            $Configs["U"]["avata"]="default";
            $Configs["U"]["email"]="";
            if(isset($_SESSION["D-USER"])){$Configs["U"]=$_SESSION["D-USER"];}
            $Configs["Foot"]=@file_get_contents(APPPATH."HTML_DOM/Foot.txt");
            $data["regions"]=  objectToArray($this->region_model->getByParent("0"));
            $this->smarty->assign('data', $data);
            $this->smarty->assign('Configs', $Configs);
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->view     (   "deal/subs/00_css"       ,"dcss"         );
            $this->smarty->view     (   "deal/subs/00_script"    ,"dscript"      );
            $this->smarty->view     (   "deal/subs/01_head"      ,"dhead"        );
            $this->smarty->view     (   "deal/subs/02_menu"      ,"dmenu"        );
            $this->smarty->view     (   "deal/subs/03_foot"      ,"dfoot"        );
            
            $this->smarty->display  (   "deal/01_home"                          );
        }
        public function point(){
            $Configs=null;
            $Configs["menu"]="customer";
            $Configs["dealtype"]="";
            $Configs["U"]["avata"]="default";
            $Configs["U"]["email"]="";
            if(isset($_SESSION["D-USER"])){$Configs["U"]=$_SESSION["D-USER"];}
            $Configs["Foot"]=@file_get_contents(APPPATH."HTML_DOM/Foot.txt");
            $data["regions"]=  objectToArray($this->region_model->getByParent("0"));
            $this->smarty->assign('data', $data);
            $this->smarty->assign('Configs', $Configs);
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->view     (   "deal/subs/00_css"       ,"dcss"         );
            $this->smarty->view     (   "deal/subs/00_script"    ,"dscript"      );
            $this->smarty->view     (   "deal/subs/01_head"      ,"dhead"        );
            $this->smarty->view     (   "deal/subs/02_menu"      ,"dmenu"        );
            $this->smarty->view     (   "deal/subs/03_foot"      ,"dfoot"        );
            
            $this->smarty->display  (   "deal/01_home"                          );
        }
         
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */